package com.may.wlrj.aop;

import javax.servlet.http.HttpSession;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.async.DeferredResult;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.may.wlrj.entity.UserEntity;
import com.may.wlrj.util.XinLongUtil;

@Component
@Aspect
public class UserRightCheck {
	
	@Pointcut("execution(* com.may.wlrj.services.UserService.C_*(..))")
	public void pointCut() {
		
	}
	
	@Around("pointCut()")
	public void around(ProceedingJoinPoint pjp) throws Throwable {
		Object[] args = pjp.getArgs();
		HttpSession session = (HttpSession)args[3];
		UserEntity sentity = (UserEntity)session.getAttribute("USER_SESSION");
		ObjectMapper mapper = new ObjectMapper(); 
    	ObjectNode resjson = mapper.createObjectNode(); 
    	String userid = "";
		DeferredResult<String> deferredResult = (DeferredResult<String>)args[0];
		try{
			com.fasterxml.jackson.databind.JsonNode rootNode = mapper.readTree(args[1].toString());		
			userid = rootNode.get("userid").asText();
			
		}catch(Exception e){
			resjson.put("status", "0");
			resjson.put("desc", "未知错误!");
			deferredResult.setResult(mapper.writeValueAsString(resjson));
    		return;
    	}
		//权限判断
    	resjson = XinLongUtil.OptAuth(userid,args[1].toString(), sentity);
		if(!resjson.get("status").asText().equals("1210")){
			deferredResult.setResult(mapper.writeValueAsString(resjson));
    		return;
		}
		try {
			pjp.proceed();			
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
}
